package com.ruoyi.good.service.impl;

import java.util.List;

import com.fasterxml.jackson.databind.JsonSerializable;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.good.domain.GoodInfo;
import com.ruoyi.good.mapper.GoodInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.good.mapper.GoodStockMapper;
import com.ruoyi.good.domain.GoodStock;
import com.ruoyi.good.service.IGoodStockService;

/**
 * 物资出入库Service业务层处理
 *
 * @author rockvan
 * @date 2023-04-09
 */
@Service
public class GoodStockServiceImpl implements IGoodStockService
{
    @Autowired
    private GoodStockMapper goodStockMapper;

    @Autowired
    private GoodInfoMapper goodInfoMapper;

    /**
     * 查询物资出入库
     *
     * @param id 物资出入库主键
     * @return 物资出入库
     */
    @Override
    public GoodStock selectGoodStockById(Long id)
    {
        return goodStockMapper.selectGoodStockById(id);
    }

    /**
     * 查询物资出入库列表
     *
     * @param goodStock 物资出入库
     * @return 物资出入库
     */
    @Override
    public List<GoodStock> selectGoodStockList(GoodStock goodStock)
    {
        return goodStockMapper.selectGoodStockList(goodStock);
    }

    /**
     * 新增物资出入库
     *
     * @param goodStock 物资出入库
     * @return 结果
     */
    @Override
    public int insertGoodStock(GoodStock goodStock)
    {
        GoodInfo goodInfo = goodInfoMapper.selectGoodInfoById(goodStock.getGoodId());
        if (goodStock.getOperateType()==1){
            if (goodInfo.getTotal()!= 0 && goodInfo.getTotal() > goodStock.getGoodNum()){
                Long num = 0l;
                num = goodInfo.getTotal() - goodStock.getGoodNum();
                goodInfo.setTotal(num);
                goodInfoMapper.updateGoodInfo(goodInfo);
            }else {
                throw new BaseException("数量小于0，不可出库");
            }

        }else {
            Long num = 0l;
            num = goodInfo.getTotal() + goodStock.getGoodNum();
            goodInfo.setTotal(num);
            goodInfoMapper.updateGoodInfo(goodInfo);
        }
        goodStock.setCreateTime(DateUtils.getNowDate());

        return goodStockMapper.insertGoodStock(goodStock);
    }

    /**
     * 修改物资出入库
     *
     * @param goodStock 物资出入库
     * @return 结果
     */
    @Override
    public int updateGoodStock(GoodStock goodStock)
    {
        goodStock.setUpdateTime(DateUtils.getNowDate());
        return goodStockMapper.updateGoodStock(goodStock);
    }

    /**
     * 批量删除物资出入库
     *
     * @param ids 需要删除的物资出入库主键
     * @return 结果
     */
    @Override
    public int deleteGoodStockByIds(Long[] ids)
    {
        return goodStockMapper.deleteGoodStockByIds(ids);
    }

    /**
     * 删除物资出入库信息
     *
     * @param id 物资出入库主键
     * @return 结果
     */
    @Override
    public int deleteGoodStockById(Long id)
    {
        return goodStockMapper.deleteGoodStockById(id);
    }
}
